package model

import (
	"community_end/utils/errmsg"
	"strconv"
)

type UserFollows struct {
	UserID   int
	FollowID int
}

func (UserFollows) TableName() string {
	return "user_follows"
}

func SelectRelationship(userID, followID string) (*UserFollows, int) {
	userfollow := &UserFollows{}
	err := db.Where("user_id = ? AND follow_id = ?", userID, followID).Find(userfollow).Error
	if err != nil {
		return &UserFollows{}, errmsg.RELATION_SELELCT_FAILED
	}
	return userfollow, errmsg.SUCCESS
}

func InsertRelationship(userID, followID string) int {
	convUserID, _ := strconv.Atoi(userID)
	convFollowID, _ := strconv.Atoi(followID)
	user, _ := SelectUserByID(convUserID)
	follow, _ := SelectUserByID(convFollowID)
	user.Follows = append(user.Follows, follow)
	user.FollowCount++

	//fmt.Println(user)
	//fmt.Println(follow)

	err := db.Save(user).Error
	if err != nil {
		return errmsg.RELATION_CREATE_FAILED
	}
	return errmsg.SUCCESS
}

func DeleteRelateByUserIdAndFollowId(userID, followID string) int {
	err := db.Where("user_id = ? AND follow_id = ?", userID, followID).Delete(&UserFollows{}).Error
	if err != nil {
		return errmsg.RELATION_DELETE_FAILED
	}
	return errmsg.SUCCESS
}
